package com.ruoyi.web.xxj.contoller;

import java.util.List;
import java.util.Objects;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.xxj.domain.ClassInfo;
import com.ruoyi.web.xxj.service.IClassInfoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 课程信息Controller
 *
 * @author ruoyi
 * @date 2024-01-24
 */
@Api(tags = "课程信息管理")
@RestController
@RequestMapping("/xxj/classInfo")
public class ClassInfoController extends BaseController {
    @Autowired
    private IClassInfoService classInfoService;

    /**
     * 查询课程信息列表
     */
    @ApiOperation("查询课程信息列表")
    @GetMapping("/list")
    public TableDataInfo<ClassInfo> list(ClassInfo classInfo) {
        startPage();
        List<ClassInfo> list = classInfoService.selectClassInfoList(classInfo);
        return getDataTable(list);
    }

    /**
     * 导出课程信息列表
     */
    @ApiOperation("导出课程信息列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, ClassInfo classInfo) {
        List<ClassInfo> list = classInfoService.selectClassInfoList(classInfo);
        ExcelUtil<ClassInfo> util = new ExcelUtil<ClassInfo>(ClassInfo.class);
        util.exportExcel(response, list, "课程信息数据");
    }

    /**
     * 获取课程信息详细信息
     */
    @ApiOperation("获取课程信息详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(classInfoService.selectClassInfoById(id));
    }

    /**
     * 新增课程信息
     */
    @ApiOperation("新增课程信息")
    @PostMapping
    public AjaxResult add(@RequestBody ClassInfo classInfo) {
        return toAjax(classInfoService.insertClassInfo(classInfo));
    }

    /**
     * 修改课程信息
     */
    @ApiOperation("修改课程信息")
    @PutMapping
    public AjaxResult edit(@RequestBody ClassInfo classInfo) {
        return toAjax(classInfoService.updateClassInfo(classInfo));
    }

    /**
     * 删除课程信息
     */
    @ApiOperation("删除课程信息")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(classInfoService.deleteClassInfoByIds(ids));
    }

    /******************************************* 以下是小程序接口 ******************************************************/
    /**
     * 查询课程信息列表
     */
    @ApiOperation("[小程序]查询课程信息列表")
    @GetMapping("/miniList")
    public TableDataInfo<ClassInfo> miniList(ClassInfo classInfo) {
    	classInfo.setStatus(String.valueOf(0));
        startPage();
        List<ClassInfo> list = classInfoService.selectClassInfoList(classInfo);
        return getDataTable(list);
    }

    @SuppressWarnings("rawtypes")
    @ApiOperation("[小程序]增加课程查看次数")
    @GetMapping("/miniAddViewCout")
    public AjaxResult addViewCount(@RequestParam(required = true, name = "课程信息主键ID") Long classInfoId) {
        ClassInfo classInfo = classInfoService.selectClassInfoById(classInfoId);
        if (Objects.isNull(classInfo)) {
            throw new ServiceException("无效课程信息主键ID！");
        }
        classInfo.setViewCount(classInfo.getViewCount() + 1);
        return toAjax(classInfoService.updateClassInfo(classInfo));
    }
}
